package pl.szpadel.android.gadu.packets;

import java.io.UnsupportedEncodingException;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;

public class SendMsg80 extends SendPacket {

	private String mPlainText;
	private int mRecipient;
	private int mSeq;
	
	public SendMsg80(long recipient, String plainText, int seq) {
		super(TYPE_SEND_MSG80);
		
		mPlainText = plainText;
		mRecipient = (int)recipient;
		mSeq = seq;
		
	}

	@Override
	protected void writeToBuffer(ByteBuffer buffer)
			throws BufferOverflowException {

		try {
			int cls = 0x0008;
			
			byte attributes[] = {0x02, 0x06, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00};
			byte plainBytes[] = mPlainText.getBytes("CP1250");
			String htmlMessage = "<span style=\"color:#000000; font-family:'MS Shell Dlg 2'; font-size:9pt; \">" + mPlainText + "</span>";
			byte htmlBytes[] = htmlMessage.getBytes("utf8");
			
			int offsetPlain = 20 + htmlBytes.length + 1;
			int offsetAttr = offsetPlain + plainBytes.length + 1;
			
			buffer.putInt(mRecipient);
			buffer.putInt(mSeq);
			buffer.putInt(cls);
			buffer.putInt(offsetPlain);
			buffer.putInt(offsetAttr);
			buffer.put(htmlBytes);
			buffer.put((byte) 0);
			buffer.put(plainBytes);
			buffer.put((byte) 0);
			buffer.put(attributes);
			
		} catch (UnsupportedEncodingException e) {
			// ?
			return;
		}
	}
	
	public String toString() {
		return "SendMsg80 [ recipient=" + mRecipient + ", text=" + mPlainText + " ]";
	}

}
